package cn.bob.org.service.impl;

import cn.bob.basic.utils.PageList;
import cn.bob.org.domain.Writer;
import cn.bob.org.mapper.WriterMapper;
import cn.bob.org.query.WriterQuery;
import cn.bob.org.service.WriterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class WriterServiceImpl implements WriterService {

    @Autowired
    private WriterMapper writerMapper;

    @Override
    public List<Writer> findAllWriter() {
        return writerMapper.findAllWriter();
    }

    @Override
    public Writer findWriterById(Long id) {
        return writerMapper.findWriterById(id);
    }

    @Override
    @Transactional
    public void deleteWriter(Long id) {
        writerMapper.deleteWriter(id);
    }

    @Override
    @Transactional
    public void addWriter(Writer writer) {
        writer.setCteatetime(new Date());
        writerMapper.addWriter(writer);
    }

    @Override
    @Transactional
    public void updateWriter(Writer writer) {
        writerMapper.updateWriter(writer);
    }

    @Override
    public PageList<Writer> queryByPage(WriterQuery query) {
        // 查询总条数
        Integer totals = writerMapper.queryByCount(query);
        if (null == totals || 0 == totals){
            return new PageList<>(new ArrayList<>(),0);
        }
        // 查询数据
        List<Writer> writers = writerMapper.queryByPage(query);
        return new PageList<>(writers,totals);
    }
}
